var gulp = require("gulp");
var uglify = require("gulp-uglify");
var browserify = require("browserify");
var sequence = require("run-sequence");
var watchify = require("watchify");
var source = require("vinyl-source-stream");
var buffer = require("vinyl-buffer");
var gif = require("gulp-if");
var coffee = require("gulp-coffee");
var concat = require("gulp-concat");
var clean = require("gulp-clean-css");
var sass = require("gulp-sass");
var fs = require("fs");
gulp.task('default',function(){ sequence("maincss"); })

gulp.task('sass',function(){
	gulp.src('css/main.scss')
	.pipe(sass())
	.pipe(gulp.dest('css/'));
})

/*
//自动构建css文件打包压缩
var gulp = require("gulp");
var uglify = require("gulp-uglify");
var browserify = require("browserify");
var sequence = require("run-sequence");
var concat = require("gulp-concat");
var clean = require("gulp-clean-css");
gulp.task('default',function(){ 
	sequence("maincss","css-watch"); 
})
gulp.task('maincss',function(){
	gulp
	.src(['css/a.css','css/b.css'])
	.pipe(concat('main.css'))
	.pipe(clean())
	.pipe(gulp.dest('css/'))
});
gulp.task('css-watch',function(){
	gulp.watch('css/*.css',['maincss'])
});*/

/*
将文件压缩打包 
var gulp = require("gulp");
var uglify = require("gulp-uglify");
var browserify = require("browserify");
var sequence = require("run-sequence");
var watchify = require("watchify");
var source = require("vinyl-source-stream");
var buffer = require("vinyl-buffer");
var gif = require("gulp-if");
var fs = require("fs");

var isProduction = process.env.ENV === 'prod';
gulp.task('default',function(){ sequence("mainjs"); })
gulp.task('mainjs',function(){
	var b = browserify({
		entries: ['assets/js/a.js','assets/js/b.js'],
		cache: {},
		packageCache :{},
		plugin: [watchify]
	});
	function bundle(){
		b
		.bundle()
		.pipe(source('main.js'))
		.pipe(buffer())
		.pipe(gif(isProduction,uglify()))
		.pipe(gulp.dest('./js/'));
	}
	bundle();
	b.on('update',bundle);
});
//输入命令  ENV=prod gulp
*/
//-------------------------------------------------------------
/*
//自己写的多个模块打包并压缩成一行
var gulp = require("gulp");
var uglify = require("gulp-uglify");
var browserify = require("browserify");
var sequence = require("run-sequence");
var watchify = require("watchify");

var source = require("vinyl-source-stream");
var buffer = require("vinyl-buffer");
var fs = require("fs");
gulp.task('default',function(){
	sequence("mainjs");
})
gulp.task('mainjs',function(){
	var b = browserify({
		entries: ['assets/js/a.js','assets/js/b.js'],
		cache: {},
		packageCache :{},
		plugin: [watchify]
	});
	function bundle(){
		b
		.bundle()
		.pipe(source('main.js'))
		.pipe(buffer())
		.pipe(uglify())
		.pipe(gulp.dest('./js/'));
	}
	bundle();
	b.on('update',bundle);
});*/
//-------------------------------------------------------------------------------------
/*//自己写的多个模块打包成main.js，引入的插件打包成一个bundle.js
var gulp = require("gulp");
var browserify = require("browserify");
//安装run-sequence  npm install run-sequence
var sequence = require("run-sequence");
//安装watchify  npm install watchify
var watchify = require("watchify");
var fs = require("fs");
gulp.task('default',function(){
	sequence("vendorjs","mainjs");
})
gulp.task('mainjs',function(){
	var b = browserify({
		entries: ['assets/js/a.js','assets/js/b.js'],
		cache: {},
		packageCache :{},
		plugin: [watchify]
	}).external('app').external('swiper');
	function bundle(){
		b.bundle().pipe(fs.createWriteStream("js/main.js"));
	}
	bundle();
	b.on('update',bundle)
});
gulp.task('vendorjs',function(){
	var b = browserify().require('./js/app.js',{
		expose: 'app'
	}).require('./js/swiper.min.js',{
		expose: 'swiper'
	}).bundle().pipe(fs.createWriteStream("bundle.js"))
})*/
//-------------------------------------------------------------------------------------
/*var gulp = require("gulp");
var browserify = require("browserify");
//安装run-sequence  npm install run-sequence
var sequence = require("run-sequence");
//安装watchify  npm install watchify
var watchify = require("watchify");
var fs = require("fs");
gulp.task('default',function(){
	sequence("mainjs");
})
gulp.task('mainjs',function(){
	var b = browserify({
		entries: ['assets/js/index.js'],
		cache: {},
		packageCache :{},
		plugin: [watchify]
	})
	function bundle(){
		b.bundle().pipe(fs.createWriteStream("js/main.js"));
	}
	bundle();
	b.on('update',bundle)
})*/

//        上面下面功能一样

/*
//多个文件打包之后 ，自动检测打包文件的改动，实现自动打包，无需再命令行输入gulp命令
var gulp = require("gulp");
var browserify = require("browserify");
var fs = require("fs");
//安装run-sequence  npm install run-sequence
var sequence = require("run-sequence");
gulp.task('default',function(){
	sequence('mainjs','watch');
})
gulp.task('mainjs',function(){
	browserify().add(['assets/js/a.js','assets/js/b.js','assets/js/index.js']).bundle().pipe(fs.createWriteStream("js/main.js"));
})
gulp.task('watch',function(){
	gulp.watch(['assets/js/*.js'],function(){
		sequence('mainjs');
	});
})*/
